package com.dz.customer.web;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import springfox.documentation.annotations.ApiIgnore;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.dz.customer.entity.Customer;
import com.dz.customer.entity.SmsResult;
import com.dz.customer.entity.SmsSenderVO;
import com.dz.customer.service.CustomerService;
import com.dz.message.service.MessageService;
import com.dz.sms.SmsProperties;
import com.dz.utils.redis.RedisUtil;
import com.qcloud.sms.SmsSingleSender;
import com.qcloud.sms.SmsSingleSenderResult;

/**
 * <p>
 * 客户基本表 前端控制器
 * </p>
 *
 * @author star
 * @since 2017-06-27
 */
@RestController
@Slf4j
public class CustomerControler {

	@Autowired
	private CustomerService customerService;

	@Autowired
	private SmsSingleSender smsSingleSender;

	@Autowired
	private SmsProperties smsProperties;

	@Autowired
	private RedisUtil redisUtils;

	@Autowired
	private MessageService messageService;

	@RequestMapping("/addCustomer")
	@ApiIgnore
	public Customer addCustomer(@RequestBody Customer customer) {
		customerService.insert(customer);
		return customer;
	}

	@RequestMapping("/getByPhone")
	@ApiIgnore
	public Long getByPhone(@RequestParam("phone") String phone) {
		return customerService.selectByPhone(phone);
	}

	@RequestMapping("/updatePhone")
	@ApiIgnore
	public boolean updatePhone(@RequestBody Customer customer) {
		Wrapper<Customer> cusWrapper = new EntityWrapper<Customer>();
		cusWrapper.eq("fid", customer.getId());
		Customer entity = new Customer();
		entity.setMobile(customer.getMobile());
		entity.setName(customer.getName());
		return customerService.update(entity, cusWrapper);
	}

	@RequestMapping("/saveWxCustInfo")
	@ApiIgnore
	public Customer saveWxInfo(@RequestBody Customer customer) {
		return customerService.saveWxUserInfo(customer);
	}

	@RequestMapping("/list")
	@ApiIgnore
	public Page<Customer> list() {
		Wrapper<Customer> cWrapper = new EntityWrapper<Customer>();
		cWrapper.orderBy("fcreateDate");
		return customerService.selectPage(new Page<Customer>(1, 10), cWrapper);
	}

	@RequestMapping("/sendMsg")
	@ApiOperation(value = "短信发送接口", httpMethod = "POST")
	public SmsResult sendCode(@RequestParam("photoNum") String phoneNum) {
		String verifyCode = org.apache.commons.lang3.RandomStringUtils.randomNumeric(4);
		String msg = String.format(smsProperties.getMsgTemplete(), verifyCode);
		SmsResult smsResult = new SmsResult();
		String code = (String) redisUtils.get("phone:" + phoneNum);
		if (code != null) {
			verifyCode = code;
		}
		try {
			if ("dev".equals(smsProperties.getMode())) {
				smsResult.setSuccess(true);
				smsResult.setVerifyCode(verifyCode);
				redisUtils.add("phone:" + phoneNum, 30L, verifyCode);
			} else {
				SmsSingleSenderResult smsSingleSenderResult = smsSingleSender.send(0, "86", phoneNum, msg, "", "");
				int resultCode = smsSingleSenderResult.getResult();
				if (resultCode == 0) {
					smsResult.setSuccess(true);
					smsResult.setVerifyCode(verifyCode);
					redisUtils.add("phone:" + phoneNum, 30L, verifyCode);
				} else {
					smsResult.setErrCode(resultCode + "");
					smsResult.setSuccess(false);
				}
			}
		} catch (Exception e) {
			log.error("send sms error:" + phoneNum, e);
			smsResult.setSuccess(false);
			smsResult.setErrMsg(e.getMessage());
		} finally {
			messageService.addMsg(phoneNum, verifyCode, smsResult.isSuccess());
		}
		return smsResult;
	}

	@RequestMapping("/verifyCode")
	@ApiIgnore
	public boolean verifyCode(@RequestBody SmsSenderVO smsSenderVO) {
		String code = (String) redisUtils.get("phone:" + smsSenderVO.getPhoneNum());
		if (code != null && code.equals(smsSenderVO.getCode())) {
			return true;
		}
		return false;
	}
}